package com.kidala.codility;

public class Gcd {

    public static void main(String[] args) {
        Gcd gcd = new Gcd();
        int result90 = gcd.gcd90(123000000, 789000000);
        int result100 = gcd.gcd90(123000000, 789000000);
        System.out.println(result90);
        System.out.println(result100);
    }

    // 90% result
    int gcd90(int a, int b) {
        int lower = a;
        int higher = b;
        if (a > b) {
            lower = b;
            higher = a;
        }

        if (higher % lower == 0) {
            return lower;
        }

        boolean k = true;
        for (int i = lower - 1; i > 1; i--) {
            int modA = a % i;
            int modB = b % i;
            if (modA == 0 && modB == 0) {
                return i;
            } else if (k) {
                i = i / 2 + 1;
                k = false;
            }
        }

        return 1;
    }

    // 100 % result
    int gcd100(int a, int b) {
        while (b != 0) {
            int c = a % b;
            a = b;
            b = c;
        }
        return a;
    }

}
